package com.ttx.pattern.example.chain.tomcat.filterchain.filter;

import com.ttx.pattern.example.chain.tomcat.filterchain.Filter;
import com.ttx.pattern.example.chain.tomcat.filterchain.FilterChain;
import com.ttx.pattern.example.chain.tomcat.filterchain.FilterConfig;
import com.ttx.pattern.example.chain.tomcat.servlet.ServletRequest;
import com.ttx.pattern.example.chain.tomcat.servlet.ServletResponse;

/**
 *
 * @author TimFruit
 * @date 19-5-12 上午10:15
 */
public class AppendAFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        String responseStr=response.getResponse();

        if(responseStr==null){
            String requestStr=request.getRequest();
            if(requestStr==null){
                requestStr="";
            }
            responseStr=requestStr;
        }

        //选择承担责任, append "A"
        responseStr=responseStr+"A";
        response.setResponse(responseStr);

        //[warning] 这里是继续调用链, 也可以选择不继续调用
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
